﻿@namespace LaserMark.Components.Components
@using LaserMark.DataAccess.Service
<dvi class="connect-line">
    <ValidateForm ShowLabel="false" Model="@ConnectLine" @ref="ValidateForm">
        <div class="row form-inline">
            <div class="col-12">
                <BootstrapLabel Value="文件夹地址"></BootstrapLabel>
                <Tooltip Title="@ConnectLine.Value">
                    <BootstrapInputGroup>
                        <BootstrapInput @bind-Value="@ConnectLine.Value"></BootstrapInput>
                        <Button Icon="fa-solid fa-file" Text="选择" OnClick="OnClick"></Button>
                    </BootstrapInputGroup>
                </Tooltip>
            </div>
        </div>
    </ValidateForm>
</dvi>

<DialogFooter OnSave="@OnSave"></DialogFooter>

@code {

    [NotNull]
    private ConnectLine? ConnectLine = new();

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }

    [Inject]
    [NotNull]
    private ISecretService? SecretService { get; set; }

    [Inject]
    [NotNull]
    private IConnectLineService? ConnectLineService { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        ConnectLine = ConnectLineService.Get("AutoFolder");
    }

    private async Task<bool> OnSave(string password)
    {
        var ret = ValidateForm.Validate();
        var res = await SecretService.GetSecret("Customer", password);
        if (res)
        {

            await ConnectLineService.Update(ConnectLine);
            return true;
        }
        else
        {
            return false;
        }
    }

    private Task OnClick()
    {
        OpenFolderDialog dialog = new OpenFolderDialog();
        dialog.DefaultDirectory = AppContext.BaseDirectory;
        dialog.InitialDirectory = AppContext.BaseDirectory;
        var ret = dialog.ShowDialog();
        if (ret.HasValue && ret.Value)
        {
            ConnectLine.Value = dialog.FolderName;
        }

        return Task.CompletedTask;
    }
}
